package com.dgut.admodule.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dgut.admodule.entity.Company;
import com.dgut.admodule.entity.dto.CompanyQueryDTO;
import com.dgut.admodule.service.ICompanyService;
import com.dgut.common.util.ExtAjaxResponse;
import com.dgut.common.util.ExtPageable;

@Controller
@RequestMapping("/company")
public class CompanyController {
	@Autowired
	private ICompanyService companyService;
	
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Company company) {
		try {
			companyService.saveOrUpdate(company);
			return new ExtAjaxResponse(true,"保存成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"保存失败！");
		}
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long id) 
	{
		try {
			Company company =  companyService.findOne(id);
			if(null!=company) {
				companyService.delete(company);
			}
			return new ExtAjaxResponse(true,"删除成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败！");
		}
	}
	
	@RequestMapping("/deleteCompanys")
	public @ResponseBody ExtAjaxResponse deleteStaffs(@RequestParam Long[] ids) 
	{
		try {
			List<Long> idLists = Arrays.asList(ids);
			if(null!=idLists) {
				companyService.delete(idLists);
			}
			return new ExtAjaxResponse(true,"删除成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败！");
		}
	}
	
	@RequestMapping("/findOne")
	public @ResponseBody Company findOne(@RequestParam Long id) 
	{
		Company company =  companyService.findOne(id);
		
		return company;
	}
	
	@RequestMapping("/findUserName")
	public @ResponseBody Company findByUserId(@RequestParam String userName) 
	{
		return companyService.findUserName(userName);
	}
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Company> findAll() 
	{
		 List<Company> lists =  companyService.findAll();
		
		return lists;
	}
	
	@RequestMapping("/findPage")
	public @ResponseBody Page<Company> findPage(CompanyQueryDTO companyQueryDTO,ExtPageable pageable) 
	{
		pageable.setSort("cId");
		Page<Company> page =  companyService.findAll(CompanyQueryDTO.getSpecification(companyQueryDTO), pageable.getPageable());
		
		return page;
	}
}
